package com.zhiyou100.weather.controller;


import com.zhiyou100.weather.mapper.CityMapper;
import com.zhiyou100.weather.pojo.Forecast;
import com.zhiyou100.weather.pojo.WeatherResponse;
import com.zhiyou100.weather.service.CityService;
import com.zhiyou100.weather.service.IWeatherDataService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/login")
public class CityController {

    @Resource
    private CityMapper cityMapper;
    @Resource
    private CityService cityService;
    @Resource
    private IWeatherDataService iWeatherDataService;

    @RequestMapping("get")
    public String get() {
        return "City";
    }

    @RequestMapping("getProvince")
    @ResponseBody
    public List getP(String cZh) {
        List pcdList = cityService.findByContryZh(cZh);
        System.out.println(pcdList);
        return pcdList;
    }

    @RequestMapping("getCity")
    @ResponseBody
    public List getC(String cZh) {
        List pcdList = cityService.findByleaderZh(cZh);
        return pcdList;
    }

    @RequestMapping("getDistrict")
    @ResponseBody
    public List getD(String cZh) {
        List pcdList = cityService.findBycityZh(cZh);
        return pcdList;
    }

    @RequestMapping("getName")
    public String getName(Model model, String cZh) {
        WeatherResponse weatherResponse = iWeatherDataService.getDataByCityName("北京");
        List<Forecast> list = weatherResponse.getData().getForecast();
        for (Forecast list1 : list) {
            String str = list1.getFengli();
            String[] str1 = str.split("A|\\[|]");
            list1.setFengli(str1[4]);
        }
        model.addAttribute("names", weatherResponse);
        return "City";
    }

    @RequestMapping("getId")
    public String getId(Model model, String cityZh) {
        if (cityZh == "") {
            WeatherResponse weatherResponse = cityService.getAll("101010100");
            List<Forecast> list = weatherResponse.getData().getForecast();
            for (Forecast list1 : list) {
                String str = list1.getFengli();
                String[] str1 = str.split("A|\\[|]");
                list1.setFengli(str1[4]);
            }
            model.addAttribute("names", weatherResponse);
            return "City";
        } else {
            WeatherResponse weatherResponse = cityService.getAll(cityZh);
            List<Forecast> list = weatherResponse.getData().getForecast();
            for (Forecast list1 : list) {
                String str = list1.getFengli();
                String[] str1 = str.split("A|\\[|]");
                list1.setFengli(str1[4]);
            }
            model.addAttribute("names", weatherResponse);
            return "City";
        }

    }

    @RequestMapping("getIdd")
    @ResponseBody
    public WeatherResponse getIdd(String cZh) {
        if (cZh == "") {

            return cityService.getAll("101010100");
        } else {

            return cityService.getAll(cZh);
        }
    }
}
